<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    

    <title>{$Think.config.site.title} - 用户管理</title>
    <meta name="keywords" content="{$Think.config.site.keywords}">
    <meta name="description" content="{$Think.config.site.description}">

    <link href="{$Think.config.site.resource_url}css/bootstrap.min.css?v=3.3.5" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/font-awesome.min.css?v=4.4.0" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/plugins/iCheck/custom.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/animate.min.css" rel="stylesheet">
    <link href="{$Think.config.site.resource_url}css/style.min.css?v=4.0.0" rel="stylesheet">
    <link href="{$Think.config.site.common_resource_url}js/chosen/chosen.css" rel="stylesheet" type="text/css"/>
</head>
<style type="text/css">
.chosen-select{
    width: 100%;
}
</style>
<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
      
      <div class="row">
          <div class="col-sm-7">
                <div class="ibox float-e-margins">
                    <div class="ibox-title">
                        <h5>{$title}</h5>
                        <div class="ibox-tools">
                            <a class="collapse-link">
                                <i class="fa fa-chevron-up"></i>
                            </a>
                      	</div>
         
                    </div>
                    <div class="ibox-content">
                        <form method="get" class="form-horizontal">
                        <input type="hidden" name="id" value="{$user['id'] ?? ''}" />
                        <input type="hidden" name="avatar" value="{$user['avatar'] ?? ''}" />
                            <div class="form-group">
                                <label class="col-sm-2 control-label">用户名</label>
                                <div class="col-sm-10">
                                    <input id="username" name="username" class="form-control" type="text" value="{$user['username'] ?? ''}">

                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" style="color:red">密 码</label>
                                <div class="col-sm-10">
                                    <input type="password" id="password" name="password" class="form-control" value="">

                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label" style="color:red">确认密码</label>
                                <div class="col-sm-10">
                                    <input type="password" id="surepassword" name="surepassword" class="form-control"  value="">

                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">职 位</label>
                                <div class="col-sm-10">
                                    <select  name="groups[]" class="chosen-select chs">
                                        <option value="0">请选择</option>
                                        {volist name="groups" id="y"}
                                        <option value="{$y.id}" {if(isset($user['groups']) && in_array($y['id'],$user['groups']) )} selected="selected" {/if} >{$y.title}</option>
                                        {/volist}
                                    </select>
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">姓 名</label>
                                <div class="col-sm-10">
                                    <input id="name" name="name" class="form-control" type="text" value="{$user['name'] ?? ''}">

                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵 称</label>
                                <div class="col-sm-10">
                                    <input id="nickname" name="nickname" class="form-control" type="text" value="{$user['nickname'] ?? ''}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">手机号码</label>
                                <div class="col-sm-10">
                                    <input id="phone" name="phone" class="form-control" type="text" value="{$user['phone'] ?? ''}">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">邮 箱</label>
                                <div class="col-sm-10">
                                    <input id="email" name="email" class="form-control" type="email" value="{$user['email'] ?? ''}">
                                </div>
                            </div>
                            <div class="hr-line-dashed"></div>
                            <div class="form-group">
                                <div class="col-sm-4 col-sm-offset-2">
     
                                    <input type="submit" class="btn btn-primary" value="提交">
                           
                                </div>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
            
            
              <div class="col-sm-5">
                  <div class="ibox float-e-margins">
                      <div class="ibox-title">
                          <h5>更多信息</h5> 
                          <div class="ibox-tools">
                                <a class="collapse-link">
                                    <i class="fa fa-chevron-up"></i>
                                </a>
                          </div>
                      </div>
                      {if ( isset($user) )}
                      <div class="ibox-content">
                        <p><img id="avatar" width=200 height=200 alt="image"  src="{$Think.cookie.avatar}" /></p>
                        <p>用户名：{$user['username']}</p>
                        <p>权限组：{$groupname}</p>
                        <p>上次登录时间：{$user['last_login_time']}</p>
                        <p>上次登录IP：{$user['last_login_ip']}</p>
                      </div>
                      {/if}
                  </div>
              </div>
          
        </div>
    </div>

    <script src="{$Think.config.site.resource_url}js/jquery.min.js?v=2.1.4"></script>
    <script src="{$Think.config.site.resource_url}js/bootstrap.min.js?v=3.3.5"></script>
    <script src="{$Think.config.site.resource_url}js/content.min.js?v=1.0.0"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/jquery.validate.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/validate/messages_zh.min.js"></script>
    <script src="{$Think.config.site.resource_url}js/plugins/layer/layer.js"></script>
    <script type="text/javascript" src="{$Think.config.site.common_resource_url}js/chosen/chosen.jquery.js"></script>
</body>

</html>
<script>
$(document).ready(function(){
    $(".chs").chosen({no_results_text: "没有找到："});
	$("form").validate({
	  debug:true,
	  submitHandler:function(form){
            save();   
      },    
	  rules: {
		username: {
		  required: true,
		  minlength: 3,
		},
		password: {
		  minlength: 6,
		},
		surepassword : {
			equalTo: "#password"
		},
		phone: {
		  number: true,
		  minlength: 6,
		},
        'groups[]':{
            required: true,
        }
	  },
	  messages: {
		username: {
		  required: '用户名必须填写',
		  minlength: '用户名不能少于3位',
		},
		password: {
		  minlength: '密码不能少于6位',
		},
		surepassword : {
			equalTo: "两次密码不同"
		},
		phone: {
		  number: '电话号码只能包含数字',
		  minlength: '电话号码不能少于6位',
		},
        'groups[]':{
            required: '用户职位必须选择',
        }
	  }
	});

	function save(){
		var fomrData = $("form").serialize();
		var action = 'add';
		if( $("input[name='id']").val() > 0) {
			action = 'edit';
		}
		$.ajax({
			type : 'POST',
			url : '<?php echo \think\Request::instance()->root(); ?>/admin/Employee/' + action,
			dataType : 'json',
			data : fomrData,
			success : function(result) {
				//layer.close(layer1);
				if(0 < result.code) {
					if(result.data) {
						//新增
						var data = result.data;
					} else {
						//修改
						var data = {
							username : $("#username").val(),
							name : $("#name").val(),
							nickname : $("#nickname").val(),
							phone : $("#phone").val(),
							avatar : $("#avatar").attr('src')
						}
					}
					parent.editSuccess(result);

				}  else  {
					//修改失败
					layer.msg(result.msg, {icon: 5});
				}
			},
			error : function(){
				layer.msg('服务器错误！请稍后重试', {icon: 2});
			}
		})
	};
})
</script>